package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import model.ExpiredQuest;

public class ExpiredQuestDao {

	public ExpiredQuestDao() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	String url = "jdbc:mysql://localhost:3306/QUESTUDY";
	String user = "root";
	String pass = "webclass";

	//String pass = "webclass";


	// 마감된 퀘스트 목록 불러오기
	public ArrayList<ExpiredQuest> getAllExpiredQuestList() throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ArrayList<ExpiredQuest> exQuestList = new ArrayList<ExpiredQuest>();
		try {

			con = DriverManager.getConnection(url, user, pass);
			pstmt = con
					.prepareStatement("select exQuestId, qid, sid, currentTime, expiredTime from expiredQuest");
			rs = pstmt.executeQuery();
			while (rs.next()) {

				int exQuestId = rs.getInt("exQuestId");
				int qid = rs.getInt("qid");
				int sid = rs.getInt("sid");
				Timestamp currentTime = rs.getTimestamp("currentTime");
				Timestamp expiredTime = rs.getTimestamp("expiredTime");
				exQuestList.add(new ExpiredQuest(exQuestId, qid, sid,
						currentTime, expiredTime));
			}

			return exQuestList;

		} catch (Exception ex) {
			ex.printStackTrace();
			return exQuestList;
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException ex) {
				}
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

	// 마감된 퀘스트 목록 불러오기
	public boolean getAllExpiredQuest(int qid) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DriverManager.getConnection(url, user, pass);
			pstmt = con
					.prepareStatement("select exQuestId, qid, sid, currentTime, expiredTime from expiredQuest where qid=?");
			pstmt.setInt(1, qid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				return true;
			} else {
				return false;
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException ex) {
				}
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

	// 마감된 퀘스트 넣기
	public void insertExpiredQuest(ExpiredQuest quest) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DriverManager.getConnection(url, user, pass);
			pstmt = con
					.prepareStatement("insert into expiredQuest(qid, sid, currentTime, expiredTime) values(?,?,sysdate(),?)");
			pstmt.setInt(1, quest.getQid());
			pstmt.setInt(2, quest.getSid());
			pstmt.setTimestamp(3, quest.getExpiredTime());
			pstmt.executeUpdate();

		} catch (Exception ex) {
			ex.printStackTrace();

		} finally {
			if (pstmt != null)
				try {
					pstmt.close();
				} catch (SQLException ex) {
				}
			if (con != null)
				try {
					con.close();
				} catch (SQLException ex) {
				}
		}

	}

}